package com.mcxx.formulcount.gonshi;


public class SetValueSentence implements ISentence
{
	private String privateScript;
	public final String getScript()
	{
		return privateScript;
	}
	public final void setScript(String value)
	{
		privateScript = value;
	}

	public final int buildSentence(java.util.ArrayList<String> arr, int pos)
	{
		if (arr.size()-pos < 3)
		{
			throw new RuntimeException("赋值语句格式错误,位置:"+pos);
		}
		setLeftVar(arr.get(pos));
		if (!arr.get(pos+1).equals("∑"))
		{
		   throw new RuntimeException("赋值语句格式错误,位置:"+pos+":"+arr.get(pos+1));
		}
		setRightExpression("");
		for (int i = pos + 2; i < arr.size(); i++)
		{
			pos =i;
			if (arr.get(i).equals(";"))
			{
				break;
			}
			setRightExpression(getRightExpression() + arr.get(i));
		}
		return pos+1;

	}
	private String privateLeftVar;
	public final String getLeftVar()
	{
		return privateLeftVar;
	}
	private void setLeftVar(String value)
	{
		privateLeftVar = value;
	}
	private String privateRightExpression;
	public final String getRightExpression()
	{
		return privateRightExpression;
	}
	private void setRightExpression(String value)
	{
		privateRightExpression = value;
	}
	public final void run(RuntimeContext ctx) throws Exception
	{
		ExpressCalculator exp = new ExpressCalculator(getRightExpression(), ctx.AllVars());
		Object val= exp.getValue();
		if (ctx.retVars.containsKey(getLeftVar()))
		{
			ctx.retVars.put(getLeftVar(), val);
		}
		else
		{
			ctx.retVars.put(getLeftVar(), val);
		}
	}
}
